/*
 * 分页
 */

var url = $('#project_log_url').val();
var project_id = $('#project_id').val();

$(function () {
    //显示模态框log内容
    $('.update_log').on('click','.show_log',function(){
        //var content = $(this).last('td').find('span').prop('title');
        var content = $(this).find('td:last-child').find('span').prop('title');
        $('#project-log-show').find('pre').text(content);
        $('.modal_log_show').trigger('click');
    })

    //点击确定模态框
    $('.modal_log_btn').click(function(){
        $(this).parent().parent().find('.close').trigger('click');
    })

    //选择日期类型
    $('select[name="date"]').change(function () {
        var value = $(this).val();
        var page = parseInt($('#current_page').val(), 10);
        var module = $('select[name="module"]').val(); //类型
        var user = $('select[name="user"]').val(); //用户
        var pageSize = parseInt($('#list .number').text(), 10);

        var param = {};
        param.date = value;
        param.module = module;
        param.user = user;
        param.page = page;
        param.pageSize = pageSize;
        param.project_id = project_id;

        package_update_log_page(url, param, page);
    });

    //选择module
    $('select[name="module"]').change(function () {
        var value = $(this).val();
        var page = parseInt($('#current_page').val(), 10);
        var date = $('select[name="date"]').val(); //查询日期
        var user = $('select[name="user"]').val(); //用户
        var pageSize = parseInt($('#list .number').text(), 10);

        var param = {};
        param.date = date;
        param.module = value;
        param.user = user;
        param.page = page;
        param.pageSize = pageSize;
        param.project_id = project_id;

        package_update_log_page(url, param, page);
    });

    //选择操作人
    $('select[name="user"]').change(function () {
        var value = $(this).val();
        var page = parseInt($('#current_page').val(), 10);
        var module = $('select[name="module"]').val(); //类型
        var date = $('select[name="date"]').val(); //查询日期
        var pageSize = parseInt($('#list .number').text(), 10);

        var param = {};
        param.date = date;
        param.module = module;
        param.user = value;
        param.page = page;
        param.pageSize = pageSize;
        param.project_id = project_id;

        package_update_log_page(url, param, page);
    });

    //选择每页显示
    $('#list .pageTotalBox').on('click', 'li', function () {
        var value = $(this).text();
        var page = parseInt($('#current_page').val(), 10);
        var date = $('select[name="date"]').val(); //查询日期
        var module = $('select[name="module"]').val(); //类型
        var user = $('select[name="user"]').val(); //用户

        var param = {};
        param.date = date;
        param.module = module;
        param.user = user;
        param.page = page;
        param.pageSize = value;
        param.project_id = project_id;

        package_update_log_page(url, param, page);
    });

    //跳转页面
    $("#rePage").blur(function () {
        var value = $(this).val();
        var date = $('select[name="date"]').val(); //查询日期
        var module = $('select[name="module"]').val(); //类型
        var user = $('select[name="user"]').val(); //用户
        var pageSize = parseInt($('#list .number').text(), 10);

        var param = {};
        param.date = date;
        param.module = module;
        param.user = user;
        param.page = value;
        param.pageSize = pageSize;
        param.project_id = project_id;

        package_update_log_page(url, param, value);
    });

});


//点击跳转页面
function re_page(page) {
    var date = $('select[name="date"]').val(); //查询日期
    var module = $('select[name="module"]').val(); //类型
    var user = $('select[name="user"]').val(); //用户
    var pageSize = parseInt($('#list .number').text(), 10);

    var param = {};
    param.date = date;
    param.module = module;
    param.user = user;
    param.page = page;
    param.pageSize = pageSize;
    param.project_id = project_id;

    package_update_log_page(url, param, page);
}


function package_update_log_page(url, param, page) {
    $.ajax({
        type: "POST",//类型 POST GET
        url: url,
        data: param,
        datatype: "json",// 返回数据的格式"xml", "html", "script", "json", "jsonp", "text".
        // 在请求之前调用的函数
        // beforeSend: function () {

        // },
        // 调用执行后调用的函数
        // complete: function (XMLHttpRequest, textStatus) {

        // },
        //成功返回之后调用的函数
        success: function (data) {
            if (data.code == 200) {
                //分页
                $('#page_list').children().remove();
                $('#page_list').html(data.data.res.pager);

                //列表数据
                $('#page_list_tr').nextAll('tr').each(function (i) {
                    $(this).remove();
                });

                var pageSize = parseInt($('#list .number').text(), 10);
                var j = pageSize * (page-1) + data.data.res.list.length;
                //var html = '';
                $.each(data.data.res.list, function (i, n) {
                    var html = '<tr class="show_log">' +
                        '<td>' + (j--) + '</td>' +
                        '<td>' + n.created_at + '</td>' +
                        '<td>' + n.user_id + '</td>' +
                        '<td>' + n.module + '</td>' +
                        '<td><span title="' + n.log + '">' + n.log2 + '</span></td>' +
                        '</tr>';

                    $('#page_list_tr').after(html);
                });

                //$('#page_list_tr').after(html);

                $('#current_page').val(page);

                $('.pagingBox').find('.totalSize').text(data.data.res.count);
            }
        },
        //调用出错执行的函数
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            layer.msg('系统错误');
        }
    });
}
